package tw.com.diray;

import tw.com.diary.ae.com.Msg;
import tw.com.diary.api.domain.MainDTO;
import tw.com.diary.api.service.MainService;
import tw.com.diary.core.MainServiceImpl;
import tw.com.diray.comm.ui.DirToast;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;

/**
 * 登入密碼
 * 
 * @author Andy
 * 
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class MainActivity extends Activity {

	private TextView password;
	private Button sumitBtn;
	

	private MainDTO dto = new MainDTO();
	private MainService service = new MainServiceImpl();


	private static final String TAG = MainActivity.class.getCanonicalName();



	

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	
		
		 

		this.password = (TextView) this.findViewById(R.id.password);
		this.sumitBtn = (Button) this.findViewById(R.id.sumit);
		

		this.sumitBtn.setEnabled(false);

		
		this.password.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.length() > 0) {
					sumitBtn.setEnabled(true);
				} else {
					sumitBtn.setEnabled(false);
				}

				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

	}

	/**
	 * 登入
	 * 
	 * @param view
	 */
	public void loginAction(View view) {
		try {
			this.dto.setPasssword(this.password.getText().toString());
			this.service.login(dto, this);

			DirToast.show(this, Msg.S0001, this.getString(R.string.loginSucessful));
			final Intent recviewIntent = new Intent(this, RecView.class);
			this.startActivity(recviewIntent);
			this.finish();
		} catch (Exception e) {
			DirToast.show(this, e);
		}

	}

	public void signGoogleAction(View view) {
		DirToast.show(this, Msg.S0001, this.getString(R.string.loginSucessful));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (item.getItemId() == R.id.action_settings) {
			Intent it = new Intent(this, ModifyPassword.class);
			this.startActivity(it);
		}

		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	
}
